package cn.xinfei.xdecision.engine.runner.service.impl;

import cn.xinfei.xdecision.common.datadao.DecisionReentryLogMapper;
import cn.xinfei.xdecision.common.datamodel.DecisionReentryLog;
import cn.xinfei.xdecision.engine.runner.service.DecisionReentryLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DecisionReentryLogServiceImpl extends ServiceImpl<DecisionReentryLogMapper, DecisionReentryLog> implements DecisionReentryLogService {

    @Autowired
    private DecisionReentryLogMapper decisionReentryLogMapper;

    @Override
    public PageInfo<DecisionReentryLog> getDecisionReentryLogByBizId(String bizId, String state, Date start, Date end, Integer pageNo, Integer pageSize) {

        PageHelper.startPage(pageNo, pageSize);


        LambdaQueryWrapper<DecisionReentryLog> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(bizId)) {
            wrapper.eq(DecisionReentryLog::getRequestId, bizId);
        }
        if (StringUtils.isNotEmpty(state)) {
            wrapper.eq(DecisionReentryLog::getState, state);
        }

        if (start != null) {
            wrapper.ge(DecisionReentryLog::getUpdateTime, start);
        }

        if (end != null) {
            wrapper.le(DecisionReentryLog::getUpdateTime, end);
        }


        List<DecisionReentryLog> DecisionReentryLogs = decisionReentryLogMapper.selectList(wrapper);

        PageInfo pageInfo = new PageInfo<>(DecisionReentryLogs);

        if (DecisionReentryLogs == null || DecisionReentryLogs.isEmpty()) {
            return null;
        }
        pageInfo.setTotal(pageInfo.getTotal());
        return pageInfo;


    }

    @Override
    public DecisionReentryLog saveDecisionReentryLogByBizId(DecisionReentryLog DecisionReentryLog) {
        int insert = decisionReentryLogMapper.insert(DecisionReentryLog);
        String decisionId = DecisionReentryLog.getDecisionId();
        log.info("saveDecisionReentryLogByBizId decisionId={},insert={}", decisionId, insert);
        return DecisionReentryLog;
    }

    @Override
    public List<DecisionReentryLog> getDecisionReentryLogByBizId(String bizId) {

        LambdaQueryWrapper<DecisionReentryLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecisionReentryLog::getRequestId, bizId);
        List<DecisionReentryLog> DecisionReentryLogs = decisionReentryLogMapper.selectList(wrapper);

        return DecisionReentryLogs;
    }

    @Override
    public List<DecisionReentryLog> getDecisionReentryLogByDecisionId(String decisionId) {

        QueryWrapper<DecisionReentryLog> wrapper = new QueryWrapper<>();
        wrapper.eq("decision_id", decisionId);
        List<DecisionReentryLog> DecisionReentryLogs = decisionReentryLogMapper.selectList(wrapper);

        return DecisionReentryLogs;
    }

    @Override
    public DecisionReentryLog updateDecisionReentryLogByDecisionId(DecisionReentryLog DecisionReentryLog) {

        String decisionId = DecisionReentryLog.getDecisionId();
        QueryWrapper<DecisionReentryLog> wrapper = new QueryWrapper<>();
        wrapper.eq("decision_id", decisionId);
        int update = decisionReentryLogMapper.update(DecisionReentryLog, wrapper);
        log.info("updateDecisionReentryLogByDecisionId decisionId={},update={}", decisionId, update);
        return DecisionReentryLog;
    }
}
